package com.wzy.Controller.admin;

import com.wzy.entity.Province;
import com.wzy.service.ProvinceService;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Set;

/**
 * @author : Zht
 * @create : 2021/2/18
 * @describe:
 */
@Controller
@RequestMapping("/admin")
public class ProvincesController {
    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 显示省份列表
     *
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/provinces")
    public String list(@RequestParam(value = "page", defaultValue = "1") Integer page, Model model) {
        model.addAttribute("pager", this.provinceService.list(page));
        return "admin/province/provinces";

    }

    //跳转到新增省份
    @GetMapping("/provinces/input")
    public String input() {
        return "admin/province/provinces-input";
    }

    //新增省份
    @PostMapping("/provinces")
    public String post(@Validated Province province, BindingResult result, RedirectAttributes attributes) {
        //清缓存
        Set<String> keys = redisTemplate.keys("*provinces*");
        redisTemplate.delete(keys);
        Province province1 = provinceService.getProvinceByName(province.getProvinceName());
        if (province1 != null) {
            result.rejectValue("provinceName", "nameError", "不能添加重复省份");
        }
        if (result.hasErrors()) {
            return "admin/provinces-input";
        }
        int count = provinceService.append(province);
        if (count == 0) {
            attributes.addFlashAttribute("message", "新增失败");
        } else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/provinces";
    }

    //跳转到编辑身份
    @GetMapping("/provinces/{provincesId}/input")
    public String editInput(@PathVariable("provincesId") Long provincesId, Model model) {
        model.addAttribute("province", this.provinceService.detail(provincesId));
        return "admin/province/provinces-modify";
    }

    //编辑省份
    @PostMapping("/provinces/{provinceId}")
    public String editPost(@Validated Province province, BindingResult result,
                           @PathVariable("provinceId") Long id, RedirectAttributes attributes) throws NotFoundException {
        //清缓存
        Set<String> keys = redisTemplate.keys("*provinces*");
        redisTemplate.delete(keys);
        Province province1 = provinceService.getProvinceByName(province.getProvinceName());
        if (province1 != null) {
            result.rejectValue("provinceName", "nameError", "不能添加重复分类");
        }
        if (result.hasErrors()) { //如果校验结果存在错误
            return "admin/province/provinces-input";
        }
        int count = provinceService.updateProvince(id, province);
        if (count == 0) {
            attributes.addFlashAttribute("message", "更新失败");
        } else {
            attributes.addFlashAttribute("message", "更新成功");
        }
        return "redirect:/admin/provinces";
    }

    //删除省份
    @RequestMapping("/provinces/{provincesId}/delete")
    public String delete(@PathVariable("provincesId") Long provincesId, RedirectAttributes attributes) {
        //清缓存
        Set<String> keys = redisTemplate.keys("*provinces*");
        redisTemplate.delete(keys);
        this.provinceService.remove(provincesId);
        attributes.addFlashAttribute("messages", "删除成功");
        return "redirect:/admin/provinces";
    }
}
